package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.domain.CheckGroup;
import com.itheima.domain.RedisConstant;
import com.itheima.domain.Setmeal;
import com.itheima.message.MessageConstant;
import com.itheima.result.PageResult;
import com.itheima.result.QueryPageBean;
import com.itheima.result.Result;
import com.itheima.service.SetmealService;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private JedisPool jedisPool;

    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile){
        try {
            /*
            获取原始文件名
             */
            String originalFilename = imgFile.getOriginalFilename();
            System.out.println(originalFilename);
            /*
            获取文件后缀
             */
            int lastIndexOf = originalFilename.lastIndexOf(".");
            String suffix = originalFilename.substring(lastIndexOf - 1);
            /*
            使用UUID随机产生文件名称，防止同名文件覆盖
             */
            String fileName=UUID.randomUUID().toString()+suffix;
            /*
            上传文件
             */
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName);
            /*
            图片上传成功
             */
            Result result = new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,fileName);
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);
            return result;
        }catch (Exception ex){
            ex.printStackTrace();
            //图片上传失败
            return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
        }
    }
    @Reference
    private SetmealService setmealService;
    @PreAuthorize("hasAuthority('CHECKITEM_ADD')")//权限校验
    @RequestMapping("/add")
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkgroupIds){
        try {
            setmealService.add(setmeal,checkgroupIds);
        }catch (Exception ex){
            return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
        }
        return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
    }
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")//权限校验
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageResult pageResult= setmealService.findPage(currentPage,pageSize,queryString);
        return pageResult;
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        Setmeal setmeal=setmealService.findById(id);
        if (setmeal!=null){
            return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,setmeal);
        }
        return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
    }

    @RequestMapping("/findCheckGroupIdsBySetmealId")
    public Result findCheckGroupIdsBySetmealId(Integer id){

        try {
            List<Integer> checkgroupIds=setmealService.findCheckGroupIdsBySetmealId(id);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,checkgroupIds);
        }catch (Exception ex){
            ex.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }
    @PreAuthorize("hasAuthority('CHECKITEM_EDIT')")//权限校验
    @RequestMapping("/edit")
    public Result edit(@RequestBody Setmeal setmeal,Integer[] checkgroupIds,String imgUrlf){
        try {
            System.out.println(checkgroupIds.length);
            setmealService.edit(setmeal,checkgroupIds,imgUrlf);
        }catch (Exception ex){
            return new Result(false, MessageConstant.EDIT_SETMEAL_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_SETMEAL_SUCCESS);
    }
    @PreAuthorize("hasAuthority('SETMEAL_DELETE')")//权限校验
    @RequestMapping("/delete")
    public Result delete(@RequestBody Setmeal setmeal){
        try {
            System.out.println(setmeal);
           setmealService.delete(setmeal);
        }catch (Exception ex){
            return new Result(false, MessageConstant.DELETE_SETMEAL_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_SETMEAL_SUCCESS);
    }
}
